package com.hh.vietnameseproject.ui

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import androidx.fragment.app.Fragment
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.databinding.ActivityMainBinding
import com.hh.vietnameseproject.logic.model.LoginData
import com.hh.vietnameseproject.ui.login.ActivityController
import com.hh.vietnameseproject.ui.me.MeFragment
import com.hh.vietnameseproject.ui.read.ReadFragment
import com.hh.vietnameseproject.ui.word.WordFragment

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding
    lateinit var data: LoginData
    lateinit var wordFragment: WordFragment
    lateinit var readFragment: ReadFragment
    lateinit var meFragment: MeFragment

    
    companion object {
        fun actionStart(context: Context, loginData: LoginData) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra("loginData", loginData)
            context.startActivity(intent)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ActivityController.addList(this)
        wordFragment = WordFragment()
        readFragment = ReadFragment()
        meFragment = MeFragment()


        supportFragmentManager.beginTransaction().add(R.id.mainContainer,wordFragment)
            .add(R.id.mainContainer,readFragment).add(R.id.mainContainer,meFragment).commit()
        hideFragment()
        supportFragmentManager.beginTransaction().show(wordFragment).commit()

        data = intent.getParcelableExtra<LoginData>("loginData")!!

        binding.bottomNav.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.word -> {
                    hideFragment()
                    supportFragmentManager.beginTransaction().show(wordFragment).commit()
                }
                R.id.read -> {
                    hideFragment()
                    supportFragmentManager.beginTransaction().show(readFragment).commit()
                }
                R.id.me ->{
                    hideFragment()
                    supportFragmentManager.beginTransaction().show(meFragment).commit()
                }
                else -> {
                }
            }
            true
        }
    }
    fun hideFragment(){
        supportFragmentManager.beginTransaction().hide(wordFragment).hide(readFragment).hide(meFragment).commit()
    }

    fun changeMeFragmentName(name:String){
        meFragment.changeName(name)
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityController.removeList(this)
    }
}